/** * A view that shows items in a vertically scrolling two-level list. This * differs from the {@link ListView} by allowing two levels: groups which can * individually be expanded to show its children. The items come from the * {@link ExpandableListAdapter} associated with this view. * <p> * Expandable lists are able to show an indicator beside each item to display * the item's current state (the states are usually one of expanded group, * collapsed group, child, or last child). Use * {@link #setChildIndicator(Drawable)} or {@link #setGroupIndicator(Drawable)} * (or the corresponding XML attributes) to set these indicators (see the docs * for each method to see additional state that each Drawable can have). The * default style for an {@link ExpandableListView} provides indicators which * will be shown next to Views given to the {@link ExpandableListView}. The * layouts android.R.layout.simple_expandable_list_item_1 and * android.R.layout.simple_expandable_list_item_2 (which should be used with * {@link SimpleCursorTreeAdapter}) contain the preferred position information * for indicators. * <p> * The context menu information set by an {@link ExpandableListView} will be a * {@link ExpandableListContextMenuInfo} object with * {@link ExpandableListContextMenuInfo#packedPosition} being a packed position * that can be used with {@link #getPackedPositionType(long)} and the other * similar methods. * <p> * <em><b>Note:</b></em> You cannot use the value <code>wrap_content</code> * for the <code>android:layout_height</code> attribute of a * ExpandableListView in XML if the parent's size is also not strictly specified * (for example, if the parent were ScrollView you could not specify * wrap_content since it also can be any length. However, you can use * wrap_content if the ExpandableListView parent has a specific size, such as * 100 pixels. * * @attr ref android.R.styleable#ExpandableListView_groupIndicator * @attr ref android.R.styleable#ExpandableListView_indicatorLeft * @attr ref android.R.styleable#ExpandableListView_indicatorRight * @attr ref android.R.styleable#ExpandableListView_childIndicator * @attr ref android.R.styleable#ExpandableListView_childIndicatorLeft * @attr ref android.R.styleable#ExpandableListView_childIndicatorRight * @attr ref android.R.styleable#ExpandableListView_childDivider * @attr ref android.R.styleable#ExpandableListView_indicatorStart * @attr ref android.R.styleable#ExpandableListView_indicatorEnd * @attr ref android.R.styleable#ExpandableListView_childIndicatorStart * @attr ref android.R.styleable#ExpandableListView_childIndicatorEnd */
@Override publicbooleanonContextItemSelected(MenuItem item){ // TODO Auto-generated method stub super.onContextItemSelected(item); switch (item.getItemId()) { case R.id.menu_item1: Toast.makeText(this, "this is first menu item", Toast.LENGTH_SHORT).show(); break; case R.id.menu_item2: Toast.makeText(this, "this is second menu item", Toast.LENGTH_SHORT).show(); break; default: break; } returnsuper.onContextItemSelected(item); }
4.4 item长按事件显示菜单并注册上下文菜单
1 2 3 4 5 6 7 8 9 10 11 12
expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override publicbooleanonItemLongClick(AdapterView<?> parent, View view, int position, long id){ if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { expandableListView.showContextMenu(); returntrue; } returnfalse; } }); registerForContextMenu(expandableListView);